//
//  UIImage+Category.m
//  SecretAlbums
//
//  Created by Tinys on 15/8/1.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "UIImage+SecretAlbums.h"

@implementation UIImage (SecretAlbums)

- (UIImage *)thumbnail
{
    CGSize thumbnailSize = CGSizeMake(157.0f, 157.0f);
    float scale = MAX(thumbnailSize.width / self.size.width, thumbnailSize.height / self.size.height);
    
    UIGraphicsBeginImageContextWithOptions(thumbnailSize, YES, 1.0f);
    
    float width = self.size.width * scale;
    float height = self.size.height * scale;
    [self drawInRect:CGRectMake((thumbnailSize.width-width)/2, (thumbnailSize.height-height)/2, width, height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

- (UIImage *)aspectRatioThumbnail
{
    // 以固定竖屏的方式取值width、height
    CGRect bounds = [[UIScreen mainScreen] bounds];
    CGSize boundsSize = CGSizeMake(MIN(bounds.size.width, bounds.size.height),
                                   MAX(bounds.size.width, bounds.size.height));
    
    float scale = MIN(boundsSize.width / self.size.width, boundsSize.height / self.size.height);
    CGSize aspectRatioThumbnailSize = CGSizeMake(self.size.width * scale,
                                                 self.size.height * scale);
    
    UIGraphicsBeginImageContextWithOptions(aspectRatioThumbnailSize, YES, 1.0f);
    
    [self drawInRect:CGRectMake(0, 0, aspectRatioThumbnailSize.width, aspectRatioThumbnailSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end
